using SqlSugar;
using IM.Easy.Core.Services;
using IM.Easy.Core.Utils;
using IM.Easy.Infrastructure.Query;
using IM.Easy.Core.Extensions;
using IM.Easy.Entity.Msg;
using IM.Easy.MsgServices.Dtos;
using IM.Easy.MsgServices.IServices;
using IM.Easy.Entity.System;

namespace IM.Easy.MsgServices.Services
{
    public class EmailAccountService : AppServices<EmailAccount>, IEmailAccountService
    {

        public EmailAccountService(ISqlSugarClient db) : base(db)
        {

        }

        /// <summary>
        /// 获取用户邮箱基本信息
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public EmailAccount GetAccount(long userId)
        {
            var emailAccount = Context.Queryable<EmailAccount>()
                                    .Where(s => s.UserId == userId)
                                    .First();
            return emailAccount;
        }


        /// <summary>
        /// 获取可用邮箱   只获取20条数据
        /// </summary>
        /// <param name="account"></param>
        /// <returns></returns>
        public List<EmailAccountDto> GetAccountList(string account)
        {
            var list = Context.Queryable<EmailAccount>()
                    .InnerJoin<SysUser>((e, u) => e.UserId == u.Id)
                  .Where(e => e.IsActive && e.Account.Contains(account))
                  .Select((e, u) => new EmailAccountDto
                  {
                      Id = e.Id,
                      UserId = e.UserId,
                      Account = e.Account,
                      NickName = u.NickName
                  }).Take(20)
                  .ToList();
            return list;
        }

        public EmailAccount Create(EmailAccount entity)
        {
            // TODO: 添加创建逻辑
            return Context.Insertable(entity).ExecuteReturnEntity();
        }

        public EmailAccount GetById(long id)
        {
            // TODO: 添加获取逻辑
            return Context.Queryable<EmailAccount>().InSingle(id);
        }

        public PageResponse<EmailAccountDto> GetPageList(EmailAccountQueryDto query)
        {
            var queryable = Context.Queryable<EmailAccount>();
            var exp = Expressionable.Create<EmailAccount>();
            exp = exp
                .And(s => s.IsActive)
                .AndIF(!string.IsNullOrEmpty(query.Account), s => s.Account.Contains(query.Account))
                .AndIF(query.Id != default, s => query.Id.Contains(s.Id));


            var page = queryable
                 .Where(exp.ToExpression())
                 .ToPageList<EmailAccount, EmailAccountDto>(query);
            return page;
        }

        public bool Update(EmailAccount entity)
        {
            // TODO: 添加更新逻辑
            return Context.Updateable(entity).ExecuteCommandHasChange();
        }

        public bool Delete(params long[] Id)
        {
            // TODO: 添加删除逻辑
            return Context.Deleteable<EmailAccount>(Id).ExecuteCommandHasChange();
        }

        /// <summary>
        /// 通过邮箱获取账号主体数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public List<SysUser> GetUser(params long[] id)
        {
            var user = Context.Queryable<EmailAccount>()
                              .InnerJoin<SysUser>((email, user) => email.UserId == user.Id)
                              .Where((email) => id.Contains(email.Id))
                              .Select((email, user) => user)
                              .ToList();
            return user;
        }
    }
}
